﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LockTime
{
    /// <summary>
    /// 控制按钮
    /// 绘制最小化，最大化，标准，关闭按钮
    /// </summary>
    public class ControlButton:Button
    {
        Color clr1;
        //颜色
        private Color color = Color.Teal;
        //悬浮色
        private Color hovercolor = Color.FromArgb(0, 0, 140);
        //点击色
        private Color clickcolor = Color.FromArgb(160, 180, 200);
        //文字坐标
        private int textX = 5;
        private int textY = 5;
        //宽和高
        private int widthC = 10;
        private int heightC = 10;
        //窗体状态
        public enum CustomFormState
        {
            Minimize,
            Normal,
            Maximize,
            Close
        }
        CustomFormState customformstate;

        //Invalidate()会触发重绘的OnPaint事件
        //窗体状态
        public CustomFormState CFormState
        {
            get { return customformstate; }
            set { customformstate = value;Invalidate(); }
        }
        //背景色
        public Color BZBackColor
        {
            get { return color; }
            set { color = value; Invalidate(); }
        }
        //鼠标悬浮色
        public Color MouseHoverColor
        {
            get { return hovercolor; }
            set { hovercolor = value; Invalidate(); }
        }
        //鼠标点击色
        public Color MouseClickColor
        {
            get { return clickcolor; }
            set { clickcolor = value; Invalidate(); }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public ControlButton()
        {
            //一些基本设置
            this.Size = new Size(20, 20);
            this.ForeColor = Color.White;
            this.FlatStyle = FlatStyle.Flat;
        }
        /// <summary>
        /// 重写鼠标进入事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            //改变悬浮色，保存原色
            clr1 = color;
            color = hovercolor;
        }
        /// <summary>
        /// 重写鼠标离开事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            //恢复原色
            color = clr1;
        }
        /// <summary>
        /// 重写鼠标按下事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            //改变点击色
            color = clickcolor;
        }
        /// <summary>
        /// 重写鼠标抬起事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            //恢复原色
            color = clr1;
        }
        /// <summary>
        /// 重写改变大小事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            //保持宽高一致
            this.Height = this.Width;
            widthC = (int)((this.Width - 2 * textX));
            heightC = (int)((this.Height - 2 * textY));
        }
        /// <summary>
        /// 重写绘制事件
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            //根据值绘制不同的按钮图案
            switch (customformstate)
            {
                case CustomFormState.Minimize://最小化
                    pevent.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
                    pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX, this.Height - textY - 2, widthC, 2);
                    break;
                case CustomFormState.Normal://标准
                    pevent.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
                    pevent.Graphics.DrawRectangle(new Pen(this.ForeColor), textX, textY, widthC, heightC);
                    pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX, textY, widthC, 2);
                    break;
                case CustomFormState.Maximize://最大化
                    pevent.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
                    pevent.Graphics.DrawRectangle(new Pen(this.ForeColor), textX + 6, textY, widthC - 6, heightC - 6);
                    pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX + 6, textY, widthC - 6, 2);
                    pevent.Graphics.DrawRectangle(new Pen(this.ForeColor), textX, textY + 6, widthC - 6, heightC - 6);
                    pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX, textY + 6, widthC - 6, 2);
                    break;
                case CustomFormState.Close://关闭
                    pevent.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
                    pevent.Graphics.DrawRectangle(new Pen(this.ForeColor), textX, textY, widthC, heightC);
                    pevent.Graphics.FillRectangle(new SolidBrush(this.ForeColor), textX, textY, widthC, 2);
                    pevent.Graphics.DrawLine(new Pen(this.ForeColor), textX, textY, textX + widthC, textY + heightC);
                    pevent.Graphics.DrawLine(new Pen(this.ForeColor), textX + widthC, textY, textX, textY + heightC);

                    break;
            }
        }
    }
}
